package Test3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test3 {
    public static void main(String[] args) {
        // 创建HashMap，键是宿舍号，值是宿舍里住的4个学生
        Map<String, List<Student>> dormitoryMap = new HashMap<>();

        // 创建宿舍1的学生
        List<Student> dorm1Students = new ArrayList<>();
        dorm1Students.add(new Student("周翔", 18));
        dorm1Students.add(new Student("王东阳", 19));
        dorm1Students.add(new Student("韩梅梅", 18));
        dorm1Students.add(new Student("李磊", 20));

        // 创建宿舍2的学生
        List<Student> dorm2Students = new ArrayList<>();
        dorm2Students.add(new Student("常闯", 19));
        dorm2Students.add(new Student("王强", 18));
        dorm2Students.add(new Student("孙玉春", 20));
        dorm2Students.add(new Student("马冬梅", 19));

        // 将宿舍信息添加到Map中
        dormitoryMap.put("宿舍101", dorm1Students);
        dormitoryMap.put("宿舍102", dorm2Students);

        // 遍历Map集合，打印每个宿舍住的学生
        System.out.println("宿舍学生分布情况：");
        System.out.println("==================");

        for (Map.Entry<String, List<Student>> entry : dormitoryMap.entrySet()) {
            String dormitoryNumber = entry.getKey();
            List<Student> students = entry.getValue();

            System.out.println(dormitoryNumber + " 住的学生：");
            for (int i = 0; i < students.size(); i++) {
                System.out.println("  " + (i + 1) + ". " + students.get(i));
            }
            System.out.println(); // 空行分隔
        }

        // 另一种遍历方式：使用entrySet和forEach
        System.out.println("使用另一种方式遍历：");
        System.out.println("==================");
        dormitoryMap.forEach((dormitoryNumber, students) -> {
            System.out.println(dormitoryNumber + " 住的学生：");
            students.forEach(student ->
                    System.out.println("  - " + student));
            System.out.println(); // 空行分隔
        });
    }

}
